<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link rel="stylesheet" href="./res/layui-v2.9.1/layui/css/layui.css">
    <style>
        .demo-reg-container {
            width: 320px;
            margin: 21px auto 0;
        }

        .demo-reg-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
</head>
<body>
<div class="layui-container">
    <h1>软帝超市管理系统（S-CMS）v1.0</h1>
    <hr>
    <div class="layui-col-md6 layui-col-md-offset3">
        <fieldset class="layui-elem-field">
            <legend>用户注册</legend>
            <div class="layui-field-box">
                <form class="layui-form">
                    <div class="demo-reg-container">
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="username" value="" lay-verify="required"
                                       placeholder="请输入账号" lay-reqtext="请填写账号" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required" placeholder="密码"
                                       autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="confirmPassword" value=""
                                       lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off"
                                       class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <input type="checkbox" name="agreement" lay-verify="required" lay-skin="primary"
                                   title="同意">
                            <a href="#terms" target="_blank" style="position: relative; top: 6px; left: -15px;">
                                <ins>用户协议</ins>
                            </a>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">注册</button>
                        </div>
                        <div class="layui-form-item demo-reg-other" style="text-align: center">
                            <span class="layui-font-gray">&copy; 软帝集团</span>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
    </div>
</div>

<script src="./res/layui-v2.9.1/layui/layui.js"></script>
<script>
    layui.use(function () {
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var util = layui.util;

        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function (value, item) {
                var passwordValue = $('#reg-password').val();
                if (value !== passwordValue) {
                    return '两次密码输入不一致';
                }
            }
        });

        // 提交事件
        form.on('submit(demo-reg)', function (data) {
            var field = data.field; // 获取表单字段值

            // 是否勾选同意
            if (!field.agreement) {
                layer.msg('您必须勾选同意用户协议才能注册');
                return false;
            }

            // 此处可执行 Ajax 等操作
            $.post('http://127.0.0.1/reg', field, function (resp) {
                if (resp.code === 0) {
                    layer.msg(resp.msg, function () {
                        location.href = 'login.html';
                    })
                } else {
                    layer.alert(resp.msg, {
                        title: '提示',
                        icon: 2
                    });
                }
            })

            return false; // 阻止默认 form 跳转
        });

    });
</script>

</body>
</html>